package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.transport.SendGoodsAreaAdapter3;
import com.sgb.kjwl.view.ui.adapter.transport.SendGoodsCityAdapter3;
import com.sgb.kjwl.view.ui.adapter.transport.SendGoodsProvinceAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration;
import com.swgk.core.util.MToast;

import java.util.Arrays;
import java.util.List;

public class SendGoodsLocationPopWindow3 extends BasePopWindow {

    private SendGoodsProvinceAdapter3 provinceAdapter;
    private SendGoodsCityAdapter3 cityAdapter;
    private SendGoodsAreaAdapter3 areaAdapter;
    private int CITY_LEVEL = 1;
    private int CITY_TOTAL_LEVEL = 2;
    private String provinceCode = "";
    private String cityCode = "";
    private String areaCode = "";
    private String provinceName = "";
    private String cityName = "";
    private String areaName = "";
    private GoodsSourceLocationEntity locationEntity;
    List<String> specialCity = Arrays.asList("北京", "天津", "上海", "重庆");
    private RecyclerView recyclerView;
    private TextView locationName, previousStep, nextStep, send_goods_sure;
    private LinearLayout bottomLayout;
    private boolean isShowCitySure;
    boolean isAllCity = true;
    boolean isShowArea = false;//是否点击回显区县
    private SureOnclick sureOnclick;
    private String simpleName, proSimple, citySimple, areaSimple;

    public boolean isShowArea() {
        return isShowArea;
    }

    public void setShowArea(boolean showArea) {
        isShowArea = showArea;
    }

    public void setSureOnclick(SureOnclick onclick) {//确定选择省市监听
        this.sureOnclick = onclick;
    }

    public void setShowCitySure(boolean showCitySure) {
        isShowCitySure = showCitySure;
    }

    public SendGoodsLocationPopWindow3(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_send_goods3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.send_goods_province_list);
        locationName = holder.getPop().findViewById(R.id.select_location_name);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        previousStep = holder.getPop().findViewById(R.id.previous_step);
        nextStep = holder.getPop().findViewById(R.id.next_step);
        send_goods_sure = holder.getPop().findViewById(R.id.send_goods_sure);
        recyclerView.addItemDecoration(new RecyclerViewItemDecoration(mContext.getResources().getDimension(R.dimen.dp10)));



        locationEntity = new GoodsSourceLocationEntity();
        //上一级
        previousStep.setOnClickListener(view -> {
            if (CITY_TOTAL_LEVEL == 3) {
                if (CITY_LEVEL == 2) {
                    recyclerView.setAdapter(provinceAdapter);
                    bottomLayout.setVisibility(View.GONE);
                    CITY_LEVEL = 1;
                    if (isShowCitySure && isAllCity) {
                        send_goods_sure.setVisibility(View.VISIBLE);
                    } else {
                        send_goods_sure.setVisibility(View.GONE);
                    }
                    locationName.setText(proSimple);
                } else if (CITY_LEVEL == 3) {
                    recyclerView.setAdapter(cityAdapter);
                    CITY_LEVEL = 2;
                    nextStep.setText("下一级");
                    if (isShowCitySure) {
                        send_goods_sure.setVisibility(View.VISIBLE);
                    }
                    locationName.setText(citySimple);
                }
            } else if (CITY_TOTAL_LEVEL == 2) {
                if (CITY_LEVEL == 2) {
                    recyclerView.setAdapter(provinceAdapter);
                    bottomLayout.setVisibility(View.GONE);
                    CITY_LEVEL = 1;
                    if (isShowCitySure && isAllCity) {
                        send_goods_sure.setVisibility(View.VISIBLE);
                    } else {
                        send_goods_sure.setVisibility(View.GONE);
                    }
                    locationName.setText(proSimple);
                }
            }
        });
        //下一级
        nextStep.setOnClickListener(view -> {
            if (CITY_TOTAL_LEVEL == 2 && CITY_LEVEL == 2) {
                //确定
                if (TextUtils.isEmpty(areaName)) {
                    ToastUtil.show(mContext, "请选择城市");
                } else {
//                    ToastUtil.show(mContext,"城市-"+cityCode);
                    locationEntity.setProvinceNo(provinceCode);
                    locationEntity.setProvinceName(provinceName);
                    locationEntity.setCityNo(cityCode);
                    locationEntity.setCityName(citySimple);
                    locationEntity.setAreaNo(areaCode);
                    locationEntity.setAreaName(areaName);
                    locationEntity.setSimpleName(simpleName);
                    onResultClick.result(locationEntity, null, null);
                    dissMiss();
                }
            } else if (CITY_TOTAL_LEVEL == 3 && CITY_LEVEL == 3) {
                //确定
                if (TextUtils.isEmpty(areaCode) && TextUtils.isEmpty(areaName)) {
                    ToastUtil.show(mContext, "请选择区县");
                } else {
//                    ToastUtil.show(mContext,"区县-"+areaCode);
                    locationEntity.setProvinceNo(provinceCode);
                    locationEntity.setProvinceName(provinceName);
                    locationEntity.setCityNo(cityCode);
                    locationEntity.setCityName(citySimple);
                    locationEntity.setAreaNo(areaCode);
                    locationEntity.setAreaName(areaName);
                    locationEntity.setSimpleName(simpleName);
                    onResultClick.result(locationEntity, null, null);
                    dissMiss();
                }
            } else if (CITY_TOTAL_LEVEL == 3 && CITY_LEVEL == 2) {
                //下一级
                if (TextUtils.isEmpty(cityCode) && TextUtils.isEmpty(cityName)) {
                    ToastUtil.show(mContext, "请选择城市");
                } else {
                    if (!TextUtils.isEmpty(cityCode)) {
                        locationName.setText(areaSimple);
                        recyclerView.setAdapter(areaAdapter);
                        CITY_LEVEL = 3;
                        nextStep.setText("确定");

                        send_goods_sure.setVisibility(View.GONE);
                        if (isShowCitySure) {
                            send_goods_sure.setVisibility(View.GONE);
                        }
                    } else {
                        locationEntity.setProvinceNo(provinceCode);
                        locationEntity.setProvinceName(provinceName);
                        locationEntity.setCityNo(cityCode);
                        locationEntity.setCityName(citySimple);
                        locationEntity.setAreaNo(areaCode);
                        locationEntity.setAreaName(areaName);
                        locationEntity.setSimpleName(simpleName);
                        onResultClick.result(locationEntity, null, null);
                        dissMiss();
                    }
                }

            }
        });
        send_goods_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确定
                if (CITY_LEVEL==2&&CITY_TOTAL_LEVEL == 2 && TextUtils.isEmpty(areaCode) && TextUtils.isEmpty(areaName)) {
                    ToastUtil.show(mContext, "请选择城市");
                } else if(CITY_LEVEL==2&&CITY_TOTAL_LEVEL == 3 && TextUtils.isEmpty(cityCode) && TextUtils.isEmpty(cityName)){
                    ToastUtil.show(mContext, "请选择城市");
                }else {
                    if (CITY_LEVEL == 1) {//省级
                        locationEntity.setProvinceNo(provinceCode);
                        locationEntity.setProvinceName(provinceName);
                        cityName = provinceName;
                        cityCode = "";
                        locationEntity.setCityNo("");
                        locationEntity.setCityName(cityName);
                        locationEntity.setAreaNo("");
                        locationEntity.setAreaName("");
                        locationEntity.setSimpleName(proSimple);
                        cityAdapter.resetCheckDatas();
                        cityAdapter.setDefItem(-1);


                    } else if (CITY_LEVEL == 2) {//市级
                        locationEntity.setProvinceNo(provinceCode);
                        locationEntity.setProvinceName(provinceName);
                        locationEntity.setCityNo(cityCode);
                        locationEntity.setCityName(cityName);
                        locationEntity.setAreaNo("");
                        locationEntity.setAreaName("");
                        locationEntity.setSimpleName(citySimple);
                        areaAdapter.resetCheckDatas();
                        areaAdapter.setDefItem(-1);
                        areaCode = "";
                        areaName = "";
                    }
                    if (sureOnclick != null) {//
                        sureOnclick.result(CITY_LEVEL);
                    }
                    onResultClick.result(locationEntity, null, null);
                    dissMiss();
                }
            }
        });

        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        provinceAdapter = new SendGoodsProvinceAdapter3(mContext, null);
        provinceAdapter.setItemSelect(true);
        provinceAdapter.setDefItem(-1);
        recyclerView.setAdapter(provinceAdapter);


        provinceAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onItemClick(AddressEntity<List<AddressEntity<List<AddressEntity>>>> provinceEntity, int position) {

                locationName.setText(provinceEntity.getSimpleName());



                if (provinceEntity.getName().equals("全国")) {
                    cityName = "全国";
                    cityCode = "";
                    provinceName = "全国";
                    provinceCode = "";
                    proSimple = "全国";
                    areaCode = "";
                    citySimple = "全国";
                    simpleName = "全国";
                    CITY_LEVEL = 1;
                    areaAdapter.setDefItem(-1);
                    return;
                }
                CITY_LEVEL = 2;
                bottomLayout.setVisibility(View.VISIBLE);

                //直辖市
                if (specialCity.contains(provinceEntity.getName())) {
                    CITY_TOTAL_LEVEL = 2;
                    nextStep.setText("确定");

                    recyclerView.setAdapter(areaAdapter);
                    areaAdapter.setDatas(provinceEntity.getChildren().get(0).getChildren());
                    cityCode = provinceEntity.getChildren().get(0).getNo();
                    cityName = provinceEntity.getChildren().get(0).getName();

                    if (!TextUtils.isEmpty(provinceCode)&&!provinceCode.equals(provinceEntity.getNo())) {
                        areaAdapter.resetCheckDatas();
                        areaAdapter.setDefItem(-1);
                        areaCode = "";
                        areaName = "";
                    }
                    send_goods_sure.setVisibility(View.GONE);
                } else {//省
                    CITY_TOTAL_LEVEL = 3;
                    nextStep.setText("下一级");
                    recyclerView.setAdapter(cityAdapter);
                    cityAdapter.setDatas(provinceEntity.getChildren());
                    if (isShowCitySure) {
                        send_goods_sure.setVisibility(View.VISIBLE);
                    }
                    if (!TextUtils.isEmpty(provinceCode)&&!provinceCode.equals(provinceEntity.getNo())) {
                        cityAdapter.resetCheckDatas();
                        cityAdapter.setDefItem(-1);
                        cityCode = "";
                        cityName = "";

                    }
                }

                    provinceCode = provinceEntity.getNo();
                    provinceName = provinceEntity.getName();
                    cityName = "";
                    citySimple = provinceEntity.getSimpleName();
                    simpleName = provinceEntity.getSimpleName();
                    proSimple = provinceEntity.getSimpleName();


            }
        });

        cityAdapter = new SendGoodsCityAdapter3(mContext, null);
        cityAdapter.setDefItem(-1);
        cityAdapter.setItemSelect(true);
        cityAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity<List<AddressEntity>>>() {
            @Override
            public void onItemClick(AddressEntity<List<AddressEntity>> provinceEntity, int position) {
                if(provinceEntity==null){
                    return;
                }
                if (provinceEntity.getName().equals("全省")) {
                    locationName.setText(proSimple);
                    cityName = provinceName;
                    simpleName = proSimple;
                    citySimple = proSimple;
                    areaSimple = proSimple;
                } else {
                    locationName.setText(provinceEntity.getSimpleName());
                    cityName = provinceEntity.getName();
                    simpleName = provinceEntity.getSimpleName();
                    citySimple = provinceEntity.getSimpleName();
                    if (!cityCode.equals(provinceEntity.getNo()))
                        areaSimple = provinceEntity.getSimpleName();
                }
                if (isShowArea) {
                    simpleName = provinceEntity.getSimpleName();
                }
                if (CITY_TOTAL_LEVEL == 3) {//有三级
                    // CITY_LEVEL = 3;
                    if (!cityCode.equals(provinceEntity.getNo())) {
                        areaAdapter.resetCheckDatas();
                        areaAdapter.setDefItem(-1);
                        areaCode = "";
                        areaName = "";
                    }
                    //  nextStep.setText("确定");
                    // recyclerView.setAdapter(areaAdapter);
                    if (provinceEntity.getChildren() != null) {
                        areaAdapter.setDatas(provinceEntity.getChildren());
                        nextStep.setText("下一级");

                    } else {
                        nextStep.setText("确定");
                        areaCode = "";
                    }
                    // send_goods_sure.setVisibility(View.GONE);
                }
                cityCode = provinceEntity.getNo();

            }
        });

        areaAdapter = new SendGoodsAreaAdapter3(mContext, null);
        areaAdapter.setDefItem(-1);
        areaAdapter.setItemSelect(true);
        areaAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity>() {
            @Override
            public void onItemClick(AddressEntity provinceEntity, int position) {
                if(provinceEntity==null){
                    return;
                }
                if (provinceEntity.getName().contains("全")) {
                    locationName.setText(citySimple);
                    areaName = citySimple;
                    areaSimple = citySimple;
                } else {
                    locationName.setText(provinceEntity.getSimpleName());
                    areaName = provinceEntity.getName();
                    areaSimple = provinceEntity.getSimpleName();
                }
                if (!isShowArea) {
                    simpleName = citySimple;
                } else {
                    simpleName = areaSimple;
                }
                areaCode = provinceEntity.getNo();
//                onResultClick.result();
            }
        });
    }

    List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> provinceEntity;

    public void setData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> list) {
        provinceEntity = list;
        provinceAdapter.setDatas(list);
    }

    public void setAllCity(boolean allCity) {
        isAllCity = allCity;
        if (!isAllCity) {
            send_goods_sure.setVisibility(View.GONE);
        }else{
            send_goods_sure.setVisibility(View.VISIBLE);
        }
    }

    public void setSelectPosition(int pro, int city, int area, String no, String name, boolean isAll) {
        isAllCity = isAll;
        provinceCode = no;
        provinceName = name;
        provinceAdapter.setDefItem(pro);
        cityAdapter.setDefItem(city);
        if (provinceEntity!=null&&provinceEntity.get(pro).getChildren() == null || provinceEntity.get(pro).getChildren().size() == 0) {
            return;
        }
        proSimple = provinceEntity.get(pro).getSimpleName();

        if (specialCity.contains(name)) {
            CITY_TOTAL_LEVEL = 2;
            CITY_LEVEL = 2;
            areaAdapter.setDefItem(area);
            areaCode = provinceEntity.get(pro).getChildren().get(0).getChildren().get(area).getNo();
            areaName = provinceEntity.get(pro).getChildren().get(0).getChildren().get(area).getName();
            areaSimple = provinceEntity.get(pro).getChildren().get(0).getChildren().get(area).getSimpleName();
            areaAdapter.setDatas(provinceEntity.get(pro).getChildren().get(0).getChildren());
            cityCode = provinceEntity.get(pro).getChildren().get(0).getNo();
            cityName = provinceEntity.get(pro).getChildren().get(0).getName();
            citySimple = provinceEntity.get(pro).getChildren().get(0).getSimpleName();
            if (isAllCity && area != 0) {
                areaSimple = provinceEntity.get(pro).getChildren().get(0).getChildren().get(area).getSimpleName();
            } else {
                areaSimple = provinceEntity.get(pro).getChildren().get(0).getSimpleName();
            }

        } else {
            CITY_TOTAL_LEVEL = 3;
            CITY_LEVEL = 3;
            areaAdapter.setDefItem(area);


            if (city!=0&& area != 0) {
                areaSimple = provinceEntity.get(pro).getChildren().get(city).getChildren().get(area).getSimpleName();
                areaCode = provinceEntity.get(pro).getChildren().get(city).getChildren().get(area).getNo();
                areaName = provinceEntity.get(pro).getChildren().get(city).getChildren().get(area).getName();

            } else if(city!=0){
                areaSimple = provinceEntity.get(pro).getChildren().get(city).getSimpleName();
                areaCode = "";
                areaName = provinceEntity.get(pro).getChildren().get(city).getName();

            }
            areaAdapter.setDatas(provinceEntity.get(pro).getChildren().get(city).getChildren());
            cityCode = provinceEntity.get(pro).getChildren().get(city).getNo();
            cityName = provinceEntity.get(pro).getChildren().get(city).getName();
            citySimple = provinceEntity.get(pro).getChildren().get(city).getSimpleName();

        }
        if(city==0&&area==0){
            cityName = proSimple;
            citySimple =proSimple;
            simpleName = proSimple;
            CITY_LEVEL = 2;
            locationName.setText(citySimple);
        }else {
            simpleName = citySimple;
            locationName.setText(areaSimple);
        }
        nextStep.setText("确定");
        cityAdapter.setDatas(provinceEntity.get(pro).getChildren());
        bottomLayout.setVisibility(View.VISIBLE);
       if(city==0&&area==0) {
           recyclerView.setAdapter(cityAdapter);
           send_goods_sure.setVisibility(View.VISIBLE);
       }else{
           recyclerView.setAdapter(areaAdapter);

           if (isAll&&CITY_LEVEL==2 ) {
               send_goods_sure.setVisibility(View.VISIBLE);
           } else {
               send_goods_sure.setVisibility(View.GONE);
           }
       }




    }

    public interface SureOnclick {
        void result(int leve);
    }
}
